/*
 * @(#)ClientImpl.java   09/09/13
 *
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 *
 *
 *
 */



package example;

import org.objectweb.fractal.api.control.BindingController;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class ClientImpl implements BindingController, Runnable {

    /** Field description */
    JButton JButton;

    /** Field description */
    JTextField JTextField;

    /** Field description */
    JFrame frame;

    /** Field description */
    String input;

    /** Field description */
    JPanel pane;

    /** Field description */
    private Service service;

    /**
     * Constructs ...
     *
     */
    public ClientImpl() {
        System.err.println("CLIENT created");
    }

    public String[] listFc() {
        return new String[] { "r" };
    }

    public Object lookupFc(final String cItf) {
        if (cItf.equals("r")) {
            return service;
        }

        return null;
    }

    public void bindFc(final String cItf, final Object sItf) {
        if (cItf.equals("r")) {
            service = (Service) sItf;
        }
    }

    public void unbindFc(final String cItf) {
        if (cItf.equals("r")) {
            service = null;
        }
    }

    public void run() {
        frame      = new JFrame();
        JTextField = new JTextField();
        JButton    = new JButton("print");
        pane       = new JPanel();

        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();

        gridBagConstraints1.fill    = GridBagConstraints.VERTICAL;
        gridBagConstraints1.gridy   = 1;
        gridBagConstraints1.weightx = 1.0;
        gridBagConstraints1.gridx   = 0;
        gridBagConstraints1.fill    = GridBagConstraints.BOTH;

        GridBagConstraints gridBagConstraints = new GridBagConstraints();

        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        frame.setSize(300, 200);
        pane.setLayout(new GridBagLayout());
        pane.add(JButton, gridBagConstraints);
        pane.add(JTextField, gridBagConstraints1);
        JButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                // TODO Auto-generated method stub
                if (e.getActionCommand().equals("print")) {
                    input = JTextField.getText();
                    service.print(input);
                }
            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(pane);
        frame.setVisible(true);
    }
}
